package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.CoalMoneyCalc;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class CoalMoneyCalcAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<CoalMoneyCalc> coalMoneyCalcList;
    private Context mContext;


    public CoalMoneyCalcAdapter(List<CoalMoneyCalc> list, Context context) {
        this.coalMoneyCalcList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (coalMoneyCalcList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coal_money_clac_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(coalMoneyCalcList.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (coalMoneyCalcList.size() > 0) {
            return coalMoneyCalcList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIV;
        ExpandView expandView;
        TextView numberTV;
        TextView statusLabelTV;
        TextView timeTV;
        TextView carsTV;
        TextView calcWeightTV;
        TextView calcMoneyTV;
        TextView infoTV;
        TextView companyTV;
        TextView coalNameTV;
        TextView sendWeightTV;
        TextView receivedWeightTV;
        TextView lossWeightTV;
        TextView addUserTV;
        TextView oauthUserTV;

        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            arrowIV = (ImageView) itemView.findViewById(R.id.arrow);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            timeTV = (TextView) itemView.findViewById(R.id.time);
            carsTV = (TextView) itemView.findViewById(R.id.cars);
            calcWeightTV = (TextView) itemView.findViewById(R.id.calc_weight);
            calcMoneyTV = (TextView) itemView.findViewById(R.id.calc_money);
            infoTV = (TextView) itemView.findViewById(R.id.info);
            companyTV = (TextView) itemView.findViewById(R.id.company);
            coalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            sendWeightTV = (TextView) itemView.findViewById(R.id.send_weight);
            receivedWeightTV = (TextView) itemView.findViewById(R.id.received_weight);
            lossWeightTV = (TextView) itemView.findViewById(R.id.loss_weight);
            addUserTV = (TextView) itemView.findViewById(R.id.add_user);
            oauthUserTV = (TextView) itemView.findViewById(R.id.oauth_user);
        }

        public void BindViewData(CoalMoneyCalc coalMoneyCalc) {
            numberTV.setText(coalMoneyCalc.getNumber());
            if (coalMoneyCalc.getType() == 1) {
                companyTV.setText(coalMoneyCalc.getBuycompany());
            } else {
                companyTV.setText(coalMoneyCalc.getSalecompany());
            }
            if (coalMoneyCalc.getPaystatus() == 1) {
                statusLabelTV.setText("已付");
            } else {
                statusLabelTV.setText("未付");
            }
            coalNameTV.setText(coalMoneyCalc.getCoaltype());
            timeTV.setText(TimeUtils.parseDateTime(coalMoneyCalc.getAddtime()));
            carsTV.setText(coalMoneyCalc.getCalccars() + "车");
            calcWeightTV.setText(coalMoneyCalc.getCalcweight() + "吨");
            calcMoneyTV.setText("金额" + coalMoneyCalc.getCalcmoney());
            String info = "";
            //1原发净重 2收货净重  3原发净重减扣吨 4收货净重减扣吨
            info += coalMoneyCalc.getSettlementtype() == 1 ? "原发净重"
                    : (coalMoneyCalc.getSettlementtype() == 2 ? "收货净重"
                    : (coalMoneyCalc.getSettlementtype() == 3 ? "原发净重减扣吨" : "收货净重减扣吨"));
            info += (coalMoneyCalc.getCalctype() == 0 ? "不考核指标" : "考核指标");
            info += (coalMoneyCalc.getIstax() == 0 ? "不含税" : "含税");
            infoTV.setText(info);
            sendWeightTV.setText("原发" + coalMoneyCalc.getSendweight() + "吨");
            receivedWeightTV.setText("收货" + coalMoneyCalc.getReceviedweight() + "吨");
            DecimalFormat df = new DecimalFormat("0.00");
            lossWeightTV.setText("磅差" + df.format(coalMoneyCalc.getSendweight() - coalMoneyCalc.getReceviedweight()) + "吨");
            addUserTV.setText("添加：" + coalMoneyCalc.getAddusername() + "\n"
                    + TimeUtils.parseDateTime(coalMoneyCalc.getAddtime()));
            if (coalMoneyCalc.getIsaudit() == 1) {
                oauthUserTV.setText("审核：" + coalMoneyCalc.getAuditusername() + "\n"
                        + TimeUtils.parseDateTime(coalMoneyCalc.getAudittime()));
            } else {
                oauthUserTV.setText("未审核");
                oauthUserTV.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_color));
            }
        }

        public void setViewListener(final int position) {
            arrowIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (expandView.isExpand()) {
                        expandView.collapse();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                    } else {
                        expandView.expand();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                    }
                }
            });
        }
    }
}
